<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use App\Http\Constant;
use App\Helpers\ValidatorResponse;
use App\Helpers\SuccessResponse;
use Illuminate\Support\Facades\DB;
use App\Http\Model\RbacNode;

class RbacNodeController extends Controller{
	public function add(Request $request){
		$validator = Validator::make($request->all(), [
			'pid' => 'required|integer|min:0',
			'name' => 'required|string|min:1',
			'path' => 'required|string',
			'type' => 'required|integer|min:1|max:3',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$rbac_node_model = new RbacNode();
			try{
				$node_id = $rbac_node_model->createNode($request);
			}catch(\Exception $e){
				return SuccessResponse::handler('['.$e->getCode().'] '.$e->getMessage());
			}
			$node_data = [
				'id' => $node_id,
				'pid' => $request->input('pid'),
				'node_name' => $request->input('name'),
				'node_path' => $request->input('path'),
				'type' => $request->input('type'),
			];
			return SuccessResponse::handler($node_data);
		}
	}

	public function edit(Request $request){
		$validator = Validator::make($request->all(), [
			'id' => 'required|integer|min:1',
			'pid' => 'required|integer|min:0',
			'name' => 'required|string|min:1',
			'path' => 'required|string',
			'type' => 'required|integer|min:1|max:3',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$rbac_node_model = new RbacNode();
			try{
				$node_id = $rbac_node_model->editNode($request);
			}catch(\Exception $e){
				return SuccessResponse::handler('['.$e->getCode().'] '.$e->getMessage());
			}
			$node_data = [
				'id' => $request->input('id'),
				'pid' => $request->input('pid'),
				'node_name' => $request->input('name'),
				'node_path' => $request->input('path'),
				'type' => $request->input('type'),
			];
			return SuccessResponse::handler($node_data);
		}
	}

	public function delete(Request $request){
		$validator = Validator::make($request->all(), [
			'id' => 'required|integer|min:1'
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			$rbac_node_model = new RbacNode();
			try{	
				$delete_node = $rbac_node_model->deleteNode($request);
			}catch(\Exception $e){
				return SuccessResponse::handler('['.$e->getCode().'] '.$e->getMessage());
			}
			if($delete_node){
				$rtn_data = [
					'id' => $request->input('id'),
				];
				return SuccessResponse::handler($rtn_data);
			}else{
				return SuccessResponse::handler('['.Constant::EC_SYSTEM_ERROR.'] system error.');
			}
		}
	}

	public function listNodes(){
		$rbac_node_model = new RbacNode();
		$rbac_nodes = $rbac_node_model->listNodes($as_navigation=false, $be_tree=true);
		return SuccessResponse::handler($rbac_nodes);
	}

	public function detail(Request $request){
		$validator = Validator::make($request->all(), [
			'id' => 'required|integer|min:1',
		]);
		if($validator->fails()){
			return ValidatorResponse::handler($validator->errors());
		}else{
			try{
				$node_data = DB::table('rbac_node')->where('id', '=', $request->input('id'))->whereIn('status', [
					Constant::TFEX_RBAC_NODE_STATUS_OPEN, Constant::TFEX_RBAC_NODE_STATUS_CLOSE
				])->get()->toArray();
				if(0 == count($node_data)){
					throw new \Exception('No rbac node [id='.$request->input('id').']', Constant::EC_RBAC_NODE_NOT_EXISTS);
				}
			}catch(\Exception $e){
				return SuccessResponse::handler('['.$e->getCode().'] '.$e->getMessage());
			}
			return SuccessResponse::handler($node_data);
		}
	}

	public function getNavigation(){
		$rbac_node_model = new RbacNode();
		$rbac_nodes = $rbac_node_model->listNodes($as_navigation=true, $be_tree=true);
		return SuccessResponse::handler($rbac_nodes);
	}
}



